package managerprojectsplugin.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.wizards.IWizardDescriptor;

import br.com.managerprojectplugin.model.Usecase;
import br.com.managerprojectplugin.wizard.UsecaseNewWizard;
import br.com.managerprojectplugin.wizard.UsecaseNewWizardPage;

public class OpenUseCaseNewWizardHandler extends AbstractHandler {
	private Usecase useCase = null;

	public Usecase getUseCase() {
		return useCase;
	}

	public void setUseCase(Usecase us) {
		this.useCase = us;
	}

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		// First see if this is a "new wizard".
		IWizardDescriptor descriptor = PlatformUI.getWorkbench().getNewWizardRegistry().findWizard(UsecaseNewWizard.WIZARD_ID);
		
		try  {
			// Then if we have a wizard, open it.
			if  (descriptor != null) {
				Usecase us = (Usecase) event.getTrigger();
				IWizard wizard = descriptor.createWizard();
				WizardDialog wd = new  WizardDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), wizard);
				wd.setTitle(wizard.getWindowTitle());
				wd.open();
				UsecaseNewWizardPage wp = (UsecaseNewWizardPage) wizard.getStartingPage();
				wp.getUc().setUseCaseParent(us);
				us.getUseCaseChildren().add(wp.getUc());
				setUseCase(us);
			}
		} catch  (CoreException e) {
			e.printStackTrace();
		}
		return null;
		
	}
}
